package com.xit.hitmouse.utils;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Handler;
import android.view.View;
import android.view.animation.CycleInterpolator;
import android.widget.Button;

import java.util.Random;

/**
 * @author 厦工 友敏612
 * @Date 2023/11/24
 */

public class CustomAnimation {
    public static void moveImage(View view, float toX, float toY , int time) {
        ObjectAnimator animX = ObjectAnimator.ofFloat(view, "x", toX);
        ObjectAnimator animY = ObjectAnimator.ofFloat(view, "y", toY);

        AnimatorSet animSetXY = new AnimatorSet();
        animSetXY.playTogether(animX, animY);
        animSetXY.setDuration(time); // 设置动画时间
        animSetXY.start();
    }

        //商店的效果
    public static  void shopAnimation(Button shopbtn){
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                int count = new Random().nextInt(4) + 1; //随机生成老鼠的数量
                if (count == 1) {
                    // 放大动画
                    ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(shopbtn, "scaleX", 1.0f, 1.5f);
                    ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(shopbtn, "scaleY", 1.0f, 1.5f);
                    scaleUpX.setDuration(500);
                    scaleUpY.setDuration(500);

// 缩小动画
                    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(shopbtn, "scaleX", 1.5f, 1.0f);
                    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(shopbtn, "scaleY", 1.5f, 1.0f);
                    scaleDownX.setDuration(500);
                    scaleDownY.setDuration(500);

// 创建放大和缩小动画的动画集合
                    AnimatorSet scaleUp = new AnimatorSet();
                    scaleUp.play(scaleUpX).with(scaleUpY);

                    AnimatorSet scaleDown = new AnimatorSet();
                    scaleDown.play(scaleDownX).with(scaleDownY).after(scaleUp);

// 启动动画
                    scaleDown.start();
                }
                if (count == 3) {
                    //后续可以根据是否有新品的设置
                    ObjectAnimator shake = ObjectAnimator.ofFloat(shopbtn, "translationX", 0f, 10f, -10f, 10f, -10f, 5f, -5f, 0f);
                    shake.setDuration(500); // 设置颤动的时间
                    shake.setInterpolator(new CycleInterpolator(5)); // 重复次数

                    shake.start();
                }
                // 随机延迟执行下一个动画
                int delay = new Random().nextInt(3000) + 1000; // 随机生成延迟时间（1秒到4秒之间）
                handler.postDelayed(this, delay);
            }

        };
        // 初始启动动画
        handler.post(runnable);
    }
}
